#!/bin/sh
#
# postinst script for pdns-server

set -e

# Load debconf
. /usr/share/debconf/confmodule

if [ -n "$PDNSDEBUG" ]; then
  echo "now debugging $0 $@"
  set -x
fi

PKGNAME="pdns-server"

PDNSCONF=/etc/powerdns/pdns.conf
PDNSDEFAULT=/etc/default/pdns
if [ -e $PDNSCONF ]; then
  PDNSDIR=`cat $PDNSCONF | grep include | awk -F '=' '{print $2}'`
fi
if [ -z "$PDNSDIR" ]; then
  PDNSDIR=/etc/powerdns/pdns.d
fi
PDNSLOCAL=$PDNSDIR/pdns.local.conf

# Temporary files
PDNSCONFTEMP=`mktemp`
PDNSLOCALTEMP=`mktemp`
PDNSDEFAULTTEMP=`mktemp`

# rename ucf-conffile. This was mostly stolen from cacti.postinst after
# a short discussion on debian-mentors, see
# http://lists.debian.org/debian-mentors/2013/07/msg00027.html
# and the following thread. Thanks to Paul Gevers
renameconffile() {
  oldname="$1"
  newname="$2"
  sourcefile="$3"
  if [ -f $oldname ] ; then
    if [ ! -e $newname ] ; then
      mv $oldname $newname
#   else: Don't do anything, leave old file in place
    fi
    ucf --purge $oldname
    ucfr --purge $PKGNAME $oldname
  elif [ ! -e $newname ] ; then
#   The file was removed, we should respect that. Unfortunately, we don't
#   have a proper way to tell ucf that for the new location, so we need
#   to hack it a bit.
#   We only need to do this if the target does not already exist. If the
#   target already exists, we can later call ucf straight as there
#   is already a version of the file available, althought never
#   provided by this package, but we can just propose the new file anyway.
    ucf --debconf-ok $sourcefile $newname
    ucfr $PKGNAME $newname
    rm -f $newname
  fi
}


case "$1" in
  configure)
    if [ -z "`getent group pdns`" ]; then
      addgroup --quiet --system pdns
    fi
    if [ -z "`getent passwd pdns`" ]; then
      echo -n "Creating user and group pdns..."
      adduser --quiet --system --home /var/spool/powerdns --shell /bin/false --ingroup pdns --disabled-password --disabled-login --gecos "PowerDNS" pdns
      echo "done"
    fi

    # Fill the temporary files with config items.
    cat /usr/share/pdns-server/pdns.conf > $PDNSCONFTEMP
    cat /usr/share/pdns-server/pdns.local.conf > $PDNSLOCALTEMP
    cat /usr/share/pdns-server/pdns > $PDNSDEFAULTTEMP

    # Do we listen on a specified address
    db_get pdns-server/localaddress || true
    if [ ! -z "$RET" ]; then
      sed -i -e "s|^\(#\)\?\(# \)\?local-address=.*$|local-address=$RET|" $PDNSCONFTEMP
    fi

    # Allow recursion subnets ?
    db_get pdns-server/allowrecursion || true
    if [ ! -z "$RET" ]; then
      sed -i -e "s|^\(#\)\?\(# \)\?allow-recursion=.*$|allow-recursion=$RET|" $PDNSCONFTEMP
    fi
    
    # this can be removed once we do not have to update from
    # wheezy (pdns 3.1) any more. Note: there was never pdns 3.2 in Debian
    if [ -n "$2" ] && dpkg --compare-versions "$2" lt 3.3; then
      renameconffile $PDNSDIR/pdns.local $PDNSDIR/pdns.local.conf $PDNSLOCALTEMP
      renameconffile $PDNSDIR/pdns.simplebind $PDNSDIR/pdns.simplebind.conf /usr/share/pdns-server/pdns.simplebind.conf
    fi

    # register configuration files with ucf
    ucfr $PKGNAME $PDNSCONF
    ucfr $PKGNAME $PDNSDEFAULT
    ucfr $PKGNAME $PDNSLOCAL
    ucfr $PKGNAME $PDNSDIR/pdns.simplebind.conf


    # Install the new configuration files if the user wants it.
    ucf --debconf-ok $PDNSCONFTEMP $PDNSCONF
    ucf --debconf-ok $PDNSDEFAULTTEMP $PDNSDEFAULT
    ucf --debconf-ok $PDNSLOCALTEMP $PDNSLOCAL
    ucf --debconf-ok /usr/share/pdns-server/pdns.simplebind.conf $PDNSDIR/pdns.simplebind.conf

    # Stop the debconf stuff
    db_stop || true

    # Clean up temporary files.
    rm -f $PDNSCONFTEMP $PDNSLOCALTEMP $PDNSDEFAULTTEMP

    # There could be passwords in these files. PowerDNS first reads the
    # configuration files and then drop root privileges.
    if [ -z "$2" ]; then
      [ -e $PDNSCONF ] && chmod 0600 $PDNSCONF
      [ -e $PDNSLOCAL ] && chmod 0600 $PDNSLOCAL
      [ -d $PDNSDIR ] && chmod 0755 $PDNSDIR
      [ -e $PDNSDEFAULT ] && chmod 0644 $PDNSDEFAULT
    fi
    
    # If we still have the default config, make sure bindbackend.conf exists
    PDNSBIND="/etc/powerdns/pdns.d/pdns.simplebind.conf"
    PDNSBINDBACKENDCONF="/etc/powerdns/bindbackend.conf"
    if [ -e "$PDNSBIND" ]; then
      if grep -q "^bind-config=$PDNSBINDBACKENDCONF$" $PDNSBIND; then
        touch $PDNSBINDBACKENDCONF
      fi
    fi

  ;;

  triggered)
    if [ -x /usr/sbin/invoke-rc.d ]; then
      invoke-rc.d pdns restart || exit $?
    else
      /etc/init.d/pdns restart || exit $?
    fi

    # Stop the debconf stuff
    db_stop || true
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
  ;;

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
  ;;
esac

# Recover from incorrect init.d script headers in version 2.9.22-1 and earlier
if [ "$1" = "configure" ] && dpkg --compare-versions "$2" le "2.9.22-1" \
	&& [ -f /etc/rc0.d/K[0-9][0-9]pdns ] \
	&& ! [ -f /etc/rc1.d/K[0-9][0-9]pdns ]; then
  update-rc.d -f pdns remove
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

